package com.nineclock.document.controller;

import com.nineclock.common.entity.PageResult;
import com.nineclock.common.entity.Result;
import com.nineclock.document.AppDocumentControllerApi;
import com.nineclock.document.dto.*;
import com.nineclock.document.service.AppDocumentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 黑马程序员
 * @Company http://www.ithiema.com
 * @Version 1.0
 */
@RestController
public class AppDocumentController implements AppDocumentControllerApi {


    @Autowired
    AppDocumentService appDocumentService;

    @Override
    @GetMapping("/listFolderAndFile")
    public Result<DocumentDto> queryFolderAndFile(
            @RequestParam(value = "parentFoldId", required = false, defaultValue = "0") Long parentFolderId) {
        DocumentDto documentDto = appDocumentService.queryFolderAndFile(parentFolderId);
        return Result.success(documentDto);
    }

    @Override
    @PostMapping("/insertFolder")
    public Result insertFolder(@RequestBody DocFolderDto docFolderDto) {
        appDocumentService.insertFolder(docFolderDto);
        return Result.success();
    }

    @Override
    @PostMapping("/insertFile")
    public Result insertFile(@RequestBody DocFileDto docFileDto) {
        appDocumentService.insertFile(docFileDto);
        return Result.success();
    }

    @Override
    @GetMapping("/getFileByFileId")
    public Result<DocFileDto> getFileByFileId(@RequestParam("id") Long fileId) {
        DocFileDto docFileDto = appDocumentService.getFileByFileId(fileId);
        return Result.success(docFileDto);
    }

    @Override
    @PostMapping("/updateFile")
    public Result updateFile(@RequestBody DocFileDto docFileDto) {
        appDocumentService.updateFile(docFileDto);
        return Result.success();
    }

    @Override
    @GetMapping("/history/{fileId}")
    public Result<List<DocFileHistoryDto>> getHistoryByFileId(@PathVariable("fileId") Long fileId) {
        List<DocFileHistoryDto> docFileHistoryDtos = appDocumentService.getHistoryByFileId(fileId);
        return Result.success(docFileHistoryDtos);
    }

    @Override
    @PutMapping("/{fileId}/{status}")
    public Result getDocFileIdAndStatus(@PathVariable("fileId") Long id,@PathVariable("status") Short status) {
        appDocumentService.getDocFileIdAndStatus(id, status);
        return Result.success();

    }



    @Override
    @PostMapping("/insertCollaboration")
    public Result insertCollaboration(@RequestBody DocCollaborationsDto docCollaborationsDto) {
        appDocumentService.insertCollaboration(docCollaborationsDto);
        return Result.success();
    }

    @GetMapping("/pagingCollaborations")
    public Result<PageResult<DocumentUserDTO>> pagingCollaborations( Long id,@RequestParam(value = "page", required = false) Long page,@RequestParam(value = "pageSize",required = false) Long pageSize){
        PageResult<DocumentUserDTO> documentUserDTOPageResult = appDocumentService.pagingCollaborations(id, page, pageSize);
        return Result.success(documentUserDTOPageResult);
    }


    @PostMapping("/deleteCollaboration")
    public Result deleteUser(@RequestBody DocCollaborationsDto docCollaborationsDto) {
        appDocumentService.deleteUser(docCollaborationsDto);
        return Result.success();
    }

    @GetMapping("/pagingUsers")
    public Result<List<DocUserCollaborationDTO>> getUserList(@RequestParam("id") Long fileId) {
        List<DocUserCollaborationDTO> data = appDocumentService.getUserList(fileId);
        return Result.success(data);
    }
}
